#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/30 15:28
@desc
"""

import json
import argparse
import setting

# JSON模板
template_json = {
    "job": {
        "content": [
            {
                "reader": {
                    "name": "postgresqlreader",
                    "parameter": {
                        "connection": [
                            {
                                "jdbcUrl": [],
                                "table": []
                            }
                        ],
                        "password": "",
                        "username": ""
                    }
                },
                "writer": {
                    "name": "hdfswriter",
                    "parameter": {
                        "column": [],
                        "compress": "",
                        "defaultFS": "",
                        "fieldDelimiter": "",
                        "fileName": "",
                        "fileType": "",
                        "path": "",
                        "writeMode": ""
                    }
                }
            }
        ],
        "setting": {
            "speed": {
                "channel": ""
            }
        }
    }
}


# 接收参数传入
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--output_json_path', type=str, default='postgresql2hive.json',
                    help='生成的DataX JSON配置文件存放路径')
parser.add_argument('--mysql_jdbc_url', type=str, default=None, help='MYSQL JDBC URL,若有多个用","隔开')

# 省略若干参数
# ...

args = parser.parse_args()
print(args)
# 填入参数
template_json['job']['content'][0]['reader']['parameter']['column'] = args.mysql_jdbc_url  # 为了健壮性可以再对参数做一些检查

# 输出JSON配置文件
with open(args.output_json_path, 'w') as f:
    f.write(json.dumps(template_json))

